package com.ridu.project.beshid.controller.api;

import com.ridu.project.beshid.api.GeneralResponse;
import com.ridu.project.beshid.entity.UserEntity;
import com.ridu.project.beshid.entity.UserExam;
import com.ridu.project.beshid.repository.UserExamRepository;
import com.ridu.project.beshid.repository.UserRepository;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * 用户答题表
 * @author 随便 2020-05-17
 */
@RestController
@RequestMapping("/api/userExam")
public class UserExamApiController {

    @Autowired
    private UserExamRepository userExamRepository;
    @Autowired
    private UserRepository userRepository;

    /**
     * 答题
     */
    @PostMapping("/saveAndFlush")
    public Object save(String subjectId, String answer, String token){
        UserEntity user = userRepository.findOneByToken(token);
        if(user != null && StringUtils.isNotBlank(user.getUsername())){
        } else {
            return GeneralResponse.error("请重新登陆");
        }
        List<UserExam> list = userExamRepository.findBySubjectIdAndUserId(subjectId, user.getId().toString());
        if(CollectionUtils.isEmpty(list)){
            UserExam ue = new UserExam();
            ue.setSubjectId(subjectId);
            ue.setAnswer(answer);
            ue.setUserId(user.getId().toString());
            userExamRepository.save(ue);
        } else {
            UserExam ue = list.get(list.size() - 1);
            ue.setAnswer(answer);
            userExamRepository.saveAndFlush(ue);
        }
        return GeneralResponse.success("成功");
    }

    /**
     * 查询
     */
    @PostMapping("/find")
    public Object find(int id){
        UserExam userExam=userExamRepository.findById(id).get();
        return GeneralResponse.success(userExam);
    }

    /**
     * 分页查询
     */
    @PostMapping("/list")
    public Object list(UserExam userExam,
                       @RequestParam(required = false, defaultValue = "0") int currentPage,
                       @RequestParam(required = false, defaultValue = "10") int pageSize) {

        //创建匹配器，需要查询条件请修改此处代码
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(currentPage - 1, pageSize, sort);
        Page<UserExam> page = userExamRepository.findAll(new Specification<UserExam>() {
            @Override
            public Predicate toPredicate(Root<UserExam> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate isDelete = null;
                isDelete = cb.equal(root.<Boolean>get("deleted"), false);
                query.where(isDelete);
                return null;
            }
        }, pageable);
        GeneralResponse response = new GeneralResponse();
        response.setRows(page.getContent());
        response.setTotal(page.getTotalElements());
        return response;
    }

}
